package com.ncs.cns.framework;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import cn.jpush.android.api.JPushInterface;

import com.ncs.cns.business.to.UserNcs;
import com.ncs.cns.framework.chat.ChattingClient;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;


public class CnsApplication extends Application {
	
	private UserNcs user;
	private boolean userPasswordRemember = true;
	private boolean firstStart = true;
    
	@Override
	public void onCreate() {
		super.onCreate();
		CrashHandler.getInstance().init(getApplicationContext());	
		SharedPreferences  sp = getSharedPreferences(Constant.SHAREPATH, Context.MODE_PRIVATE);
		if(sp.getBoolean(Constant.ACCEPT_PUSH, true)){
			JPushInterface.resumePush(this);
		}
		else{
			JPushInterface.stopPush(this);			
		}
		JPushInterface.setLatestNotifactionNumber(this, 1);
		initLoginParams();
		initImageLoader(getApplicationContext());
		new Thread(){
			public void run() {
				ChattingClient.getInstance(CnsApplication.this);
			};
		}.start();
	}
	
	private void initLoginParams() {
		SharedPreferences  sp = getSharedPreferences(Constant.SHAREPATH, Context.MODE_PRIVATE);
		String userId = sp.getString(Constant.USERID_COOKIE, null);
		String userPwd = sp.getString(Constant.USERPASSWORD_COOKIE, null);
		boolean userPwdRemember = sp.getBoolean(Constant.USERPASSWORDREMEMBER_COOKIE, true);
		firstStart = sp.getBoolean(Constant.FIRSTSTART, true);
		if(userId != null){
			user = new UserNcs();
			user.setUserId(userId);
			if(userPwdRemember){
				user.setPassword(userPwd);
			}else{
				user.setPassword("");
			}
			userPasswordRemember = userPwdRemember;
		}
	}
	
	private void initImageLoader(Context context) {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.build();
		ImageLoader.getInstance().init(config);
	}
	
	public void clearLoginParams() {
		SharedPreferences  sp = getSharedPreferences(Constant.SHAREPATH, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.clear();
		editor.commit();
		user = null;
	}

	public UserNcs getUser() {
		return user;
	}

	public void setUser(UserNcs user) {
		this.user = user;
	}

	public boolean isUserPasswordRemember() {
		return userPasswordRemember;
	}

	public void setUserPasswordRemember(boolean userPasswordRemember) {
		this.userPasswordRemember = userPasswordRemember;
	}

	public boolean isFirstStart() {
		return firstStart;
	}

	public void setFirstStart(boolean firstStart) {
		this.firstStart = firstStart;
	}
	
}

